/**  
 * All rights Reserved, Designed By www.tokenmaster.io
 * @Title:  TradeLogic.java   
 * @Package com.tokenassistant.trade   
 * @author: 橘狮互联网科技有限公司 
 * @date:   2018年6月10日 下午7:21:09   
 * @version V1.0 
 * @Copyright: 2018 www.tokenmaster.io Inc. All rights reserved. 
 * 注意：本内容仅限于橘狮互联网科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package com.tokenmaster.aitrade.trade;

import org.apache.log4j.Logger;

import com.tokenmaster.aitrade.trade.boll.Index_Boll_Info;

/**
 * @ClassName: TradeLogic
 * @Description: 自动交易逻辑
 * @author buxinyun
 * @date 2018年6月10日 下午7:21:09
 * 
 */
public class TradeLogic {

	private static TradeLogic instance = new TradeLogic();

	public static TradeLogic getInstance() {
		return instance;
	}

	private Logger logger = Logger.getLogger(this.getClass());

	private double usdtNum = 10000;
	private double coinNum = 0;

	/**
	 * 根据布林线进行买卖
	 * 
	 * @param market
	 * @param symbol
	 * @param contract_type
	 * @param type
	 * @param index_Boll_Info
	 */
	public void tradeByBoll(String market, String symbol, String contract_type, String type,
			Index_Boll_Info index_Boll_Info) {

		if (!type.equals("1hour"))
			return;

		int posiition = index_Boll_Info.getPosition();

		switch (posiition) {
		case 1:
			buyCoin(index_Boll_Info.getCurrentPrice(), 2000);
			break;
		case 2:
			buyCoin(index_Boll_Info.getCurrentPrice(), 1000);
			break;
		case 5:
			sellCoin(index_Boll_Info.getCurrentPrice(), 1000);
			break;
		case 6:
			sellCoin(index_Boll_Info.getCurrentPrice(), 2000);
			break;

		default:
			break;
		}

		logger.info("账户情况总资产:" + (coinNum * index_Boll_Info.getCurrentPrice() + usdtNum) + " USDT: " + usdtNum
				+ " BTC: " + String.format("%.3f", coinNum));

	}

	/**
	 * @param i
	 * 
	 */
	private void buyCoin(double price, double cost) {

		if (usdtNum < cost)
			return;

		logger.info("买入,当前价格:" + price);
		// 购买数量
		double amount = cost / price;

		usdtNum = usdtNum - cost;

		coinNum = coinNum + amount * 0.998;

	}

	/**
	 * @param i
	 */
	private void sellCoin(double price, double cost) {
		// 卖出数量
		double amount = cost / price;

		if (coinNum < amount)
			return;

		logger.info("卖出,当前价格:" + price);

		usdtNum = usdtNum + cost * 0.998;

		coinNum = coinNum - amount;

	}
}
